﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity.Validation;

namespace EduProject.Areas.User.Models
{
    
    public class OperateOrder
    {
        BShopEntities shopEntity = new BShopEntities();
        public int tempId = 0;
        public int InsertToOrder(Order orderInfo)
        {
            try
            {
                var OrderItem = new Order
                {
                    TranTime = orderInfo.TranTime,
                    CName = orderInfo.CName,
                    CPhone = orderInfo.CPhone,
                    CAddress = orderInfo.CAddress,
                    Status = orderInfo.Status,
                    OrderDate = orderInfo.OrderDate,
                    UserUName = orderInfo.UserUName,
                    TotalPrice=orderInfo.TotalPrice,
                    AllCount=orderInfo.AllCount
                };
                shopEntity.Order.Add(OrderItem);
                shopEntity.SaveChanges();
                tempId = OrderItem.Id;
            }
            catch (DbEntityValidationException dvEx)
            {
                
               
            }
            return tempId;
            
        }

        public void InsertIntoOrderDetail(List<Cart> list, int Orderid)
        {
            var OrderDetailItem = new OrderDetail();
            try
            {
                foreach (var item in list)
                {
                    OrderDetailItem.OrderId = Orderid;
                    OrderDetailItem.PName = item.PName.Trim();
                    OrderDetailItem.Price = item.Price;
                    OrderDetailItem.mlNum = item.mlNum.Trim();
                    OrderDetailItem.Count = item.Count;
                    OrderDetailItem.pic = item.image;
                    shopEntity.OrderDetail.Add(OrderDetailItem);
                    shopEntity.SaveChanges();
                }
            }
            catch (DbEntityValidationException dbex)
            {
            }
           
        }
    }
}